﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Ks.Fs.Core.Common;
using Ks.Fs.Entity;

namespace Ks.Fs.Core.Services
{
    public class FileService : BaseService
    {        
        public static int WriteFile(Stream source,Entity.File objFile,out string path)
        {
            int resultId = 0;
            string RootFolder = Config.RootPath;                        
            Folder objFolder = FolderService.GetFolder(objFile.FolderId);                                         

            if(objFolder != null)
            {
                objFile.Path = Path.Combine(objFolder.Path, objFile.Filename);
            }            
            else
            {
                objFile.Path = objFile.Filename;
            }
                        
            //write file to disk 
            FileStream targetStream = null;
            using (targetStream = new FileStream(Path.Combine(RootFolder,objFile.Path), FileMode.Create,
                                  FileAccess.Write, FileShare.None))
            {                
                //read from the input stream in 65000 byte chunks
                const int bufferLen = 65000;
                byte[] buffer = new byte[bufferLen];
                int count = 0;
                while ((count = source.Read(buffer, 0, bufferLen)) > 0)
                {
                    // save to output stream
                    targetStream.Write(buffer, 0, count);               
                }
                targetStream.Close();
                source.Close();
                path = objFile.Path;
                resultId = DataProvider.SaveOrUpdateFile(objFile);
            }

            if (resultId > 0)
            {                          
                //make a thumb image 
                string imageThumbFile = Config.RootPath + "//" + Config.ThumbFolderName + "//" + objFile.Path;
                ThumbHelper.GenThumbImage(Path.Combine(RootFolder, objFile.Path),imageThumbFile,Config.ImageThumbWidth);
            }

            return resultId;
                        
        }        

        public static bool Move(int fileId, int ToFolderId)
        {
            string fullSourceFile = "";
            string fullDestinationFile = "";

            Entity.File objFile = DataProvider.GetFile(fileId);
            if (objFile != null)
            {
                //source
                Folder objFolder = FolderService.GetFolder(objFile.FolderId);                
                if (objFolder != null)
                {
                    fullSourceFile = Path.Combine(Config.RootPath, objFolder.Path, objFile.Path);
                }
                else
                {
                    fullSourceFile = Path.Combine(Config.RootPath, objFile.Path);
                }

                //destination
                Folder objToFolder = DataProvider.GetFolder(ToFolderId);
                if (objToFolder != null)
                {
                    fullDestinationFile = Path.Combine(Config.RootPath, objToFolder.Path, objFile.Path);
                }
                else
                {
                    fullDestinationFile = Path.Combine(Config.RootPath, objFile.Path);
                }

                System.IO.File.Move(fullSourceFile,fullDestinationFile);

                objFile.FolderId = ToFolderId;
                return (DataProvider.SaveOrUpdateFile(objFile) > 0);
            }            
            
            return false;
        }

        public static bool Copy(int fileId, int ToFolderId)  
        {
            string fullSourceFile = "";
            string fullDestinationFile = "";

            Entity.File objFile = DataProvider.GetFile(fileId);
            if (objFile != null)
            {
                //source
                Folder objFolder = FolderService.GetFolder(objFile.FolderId);
                if (objFolder != null)
                {
                    fullSourceFile = Path.Combine(Config.RootPath, objFolder.Path, objFile.Path);
                }
                else
                {
                    fullSourceFile = Path.Combine(Config.RootPath, objFile.Path);
                }

                //destination
                Folder objToFolder = DataProvider.GetFolder(ToFolderId);
                if (objToFolder != null)
                {
                    fullDestinationFile = Path.Combine(Config.RootPath, objToFolder.Path, objFile.Path);
                }
                else
                {
                    fullDestinationFile = Path.Combine(Config.RootPath, objFile.Path);
                }

                System.IO.File.Copy(fullSourceFile, fullDestinationFile,true);

                objFile.FolderId = ToFolderId;

                return (DataProvider.SaveOrUpdateFile(objFile) > 0);
            }

            return false;
        }

        public static bool DeleteFile(int FileId)
        {
            Entity.File objFile = DataProvider.GetFile(FileId);
            if (objFile != null)
            {
                string fullSourceFile = "";
                //source
                Folder objFolder = FolderService.GetFolder(objFile.FolderId);

                if (objFolder != null)
                {
                    fullSourceFile = Path.Combine(Config.RootPath, objFolder.Path, objFile.Filename);
                }
                else
                {
                    fullSourceFile = Path.Combine(Config.RootPath, objFile.Filename);
                }

                System.IO.File.Delete(fullSourceFile);

                return DataProvider.DeleteFile(FileId);

            }

            return false;
        }

        public static List<Entity.File> GetFiles(int folderId, int SortBy, bool IsSortAscending, int PageSize, int PageIndex)
        {
            return DataProvider.GetFiles(folderId,SortBy,IsSortAscending,PageSize,PageIndex);
        }
    }
}
